using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BouncinBall.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Paddle : VisibleComponent
    {
        #region Constants

        public const string TEXTURE_FILENAME = "paddle_vert_480x632";
        public const float PADDLE_THICKNESS = 0.4F;

        #endregion Constants

        #region Properties

        public Utility.Position Position { get; set; }
        public Utility.Velocity Velocity { get; set; }
        public float Diameter { get; set; } //in meters
        private float Width { get; set; } //in meters
        private float Height { get; set; } //in meters
        private PaddleType _type;

        public Vector3 OrientationLatitudinalVector { get; set; }
        public Vector3 OrientationLongitudinalVector { get; set; }
        public Vector3 OrientationNormalVector { get; set; }

        //public Vector3 OrientationLatitudinalVector
        //{
        //    get
        //    {
        //        return new Vector3(1, 0, 0); //NOTE: return based on orientation
        //    }
        //}
        //private Vector3 _latVec;

        //public Vector3 OrientationLongitudinalVector
        //{
        //    get
        //    {
        //        return new Vector3(0, 1, 0); //NOTE: return based on orientation
        //    }
        //}
        //private Vector3 _longVec;

        //public Vector3 OrientationNormalVector
        //{
        //    get
        //    {
        //        return new Vector3(0, 0, 1); //NOTE: return based on orientation
        //    }
        //}
        //private Vector3 _normVec;

        public BoundingBox BoundingShape
        {
            get
            {
                //Vector3 positionOffset = new Vector3(Width / 2, Height / 2, PADDLE_THICKNESS / 2); //assumes paddle held parallel
                Vector3 positionOffset = Width / 2 * Utility.Helper.DotProduct(OrientationLatitudinalVector, new Vector3(1, 0, 0)) * new Vector3(1, 0, 0) //width components
                    + Width / 2 * Utility.Helper.DotProduct(OrientationLatitudinalVector, new Vector3(0, 1, 0)) * new Vector3(0, 1, 0)
                    + Width / 2 * Utility.Helper.DotProduct(OrientationLatitudinalVector, new Vector3(0, 0, 1)) * new Vector3(0, 0, 1)
                    + Height / 2 * Utility.Helper.DotProduct(OrientationLongitudinalVector, new Vector3(1, 0, 0)) * new Vector3(1, 0, 0) //height components
                    + Height / 2 * Utility.Helper.DotProduct(OrientationLongitudinalVector, new Vector3(0, 1, 0)) * new Vector3(0, 1, 0)
                    + Height / 2 * Utility.Helper.DotProduct(OrientationLongitudinalVector, new Vector3(0, 0, 1)) * new Vector3(0, 0, 1)
                    + PADDLE_THICKNESS / 2 * Utility.Helper.DotProduct(OrientationNormalVector, new Vector3(1, 0, 0)) * new Vector3(1, 0, 0) //thickness components
                    + PADDLE_THICKNESS / 2 * Utility.Helper.DotProduct(OrientationNormalVector, new Vector3(0, 1, 0)) * new Vector3(0, 1, 0)
                    + PADDLE_THICKNESS / 2 * Utility.Helper.DotProduct(OrientationNormalVector, new Vector3(0, 0, 1)) * new Vector3(0, 0, 1);

                return new BoundingBox(Position.FullPosition - positionOffset, Position.FullPosition + positionOffset);
                //return new BoundingSphere(Position.FullPosition, Diameter);
            }
        }

        #endregion Properties

        #region Enumerations

        public enum PaddleType
        {
            Circle,
            Rectangle
        }

        #endregion Enumerations

        #region Constructors

        public Paddle(Game game)
            : base(game, TEXTURE_FILENAME, BouncingBallGame.PADDLE_DEPTH)
        {
            // TODO: Construct any child components here
            Position = new Utility.Position();
            Velocity = new Utility.Velocity();
            Width = Height = 0;

            OrientationLatitudinalVector = new Vector3(1, 0, 0);
            OrientationLongitudinalVector = new Vector3(0, 1, 0);
            OrientationNormalVector = new Vector3(0, 0, 1);
        }

        public Paddle(Game game, float diameter)
            : this(game)
        {
            _type = PaddleType.Circle;
            Width = Height = Diameter = diameter;
        }

        public Paddle(Game game, float height, float width)
            : this(game)
        {
            _type = PaddleType.Rectangle;
            Width = width;
            Height = height;
            Diameter = Math.Max(width, height);
        }

        #endregion Constructors

        #region Overrides

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            if (_type == PaddleType.Circle)
                SetSpriteScale(Diameter);
            else
                SetSpriteScale(Width);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);

            if (((BouncingBallGame)Game).MotionActive)
            {
                //update position/velocity/orientation based on sensors
                MotionReading reading = ((BouncingBallGame)Game).MotionSensorReading;

                // Get the RotationMatrix from the MotionReading, multiple by "flat" normal vector
                OrientationLatitudinalVector = Utility.Helper.RotateVector(reading.Attitude.RotationMatrix, new Vector3(1, 0, 0));
                OrientationLongitudinalVector = Utility.Helper.RotateVector(reading.Attitude.RotationMatrix, new Vector3(0, 1, 0));
                OrientationNormalVector = Utility.Helper.RotateVector(reading.Attitude.RotationMatrix, new Vector3(0, 0, 1));

                //apply acceleration / velocity
                Velocity.ApplyAcceleration(reading.DeviceAcceleration, (float)gameTime.ElapsedGameTime.TotalSeconds);
                Position.ApplyVelocity(Velocity, (float)gameTime.ElapsedGameTime.TotalSeconds);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        #endregion Overrides
    }
}
